﻿package igz.fleaxy.fx {
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import com.greensock.easing.Back;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Elastic;
	import igz.fleaxy.util.ImageUtil;
	import igz.fleaxy.util.ObjectUtil;
	import com.greensock.TweenMax;

	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class FxGrowBounce {

		/**
		 * Íntroduce en el elemento un efecto de "rebote" lo mueve y agranda con efecto rebote
		 * @param	p_displayObject Objeto al que se le realiza la transformacións
		 * @param	p_settings _Settings : Object = {
								"duration" :2 //segundos
								,"ease" :Back.easeIn
								,"delay" : 1 //segundos 
								,"onComplete": funcion que se ejecuta al terminar
							};
		 */
		static public function $Start(p_displayObject:DisplayObject, p_settings:Object = null) : void {
			new FxGrowBounce()._StartGrow(p_displayObject, p_settings);
		}


		protected var _Settings:Object = { duration : 2
										 , delay : 0
										 , ease : Elastic.easeOut
										 , onComplete : null
										 };

		public function FxGrowBounce() {

		}

		/**
		 * Introduce en el elemento un efecto de "rebote" lo mueve y agranda con efecto rebote
		 * 
		 * @param	p_displayObject Objeto al que se le aplica un efecto
		 * @param	p_settings _Settings : Object = {
								"duration" :2 //segundos
								,"ease" :Back.easeIn
								,"delay" : 1 //segundos 
								,"onComplete": funcion que se ejecuta al terminar
							};
		 */
		protected function _StartGrow(p_displayObject:DisplayObject, p_settings:Object = null) : void {
			ObjectUtil.$Merge(p_settings, _Settings);

			var bmp:Bitmap = ImageUtil.$GetBitmap(p_displayObject);
			bmp.x = p_displayObject.x + (bmp.width/2);
			bmp.y = p_displayObject.y + (bmp.height/2);
			bmp.scaleX = 0;
			bmp.scaleY = 0;
			p_displayObject.parent.addChild(bmp);

			p_displayObject.visible = false;

			var xEnd:Number = p_displayObject.x;
			var yEnd:Number = p_displayObject.y;

			TweenMax.to(bmp, _Settings["duration"], { x : xEnd, y : yEnd
										 , scaleX : 1, scaleY : 1
										 , onComplete : _Grow_OnComplete
										 , onCompleteParams : [p_displayObject, bmp] 
										 , ease : _Settings["ease"]
										 , delay : _Settings["delay"]
										 } 
						);
		}
		/**
		*	 Se ejecuta al finalizaar la animación
		* @param	p_displayObject displayObject donde se realiza
		* @param	p_bmp Bitmap donde se realiza el cambio
		*/
		protected function _Grow_OnComplete(p_displayObject:DisplayObject, p_bmp:Bitmap) : void {
			p_displayObject.visible = true;
			p_bmp.parent.removeChild(p_bmp);
			p_bmp = null;
			if ( _Settings["onComplete"] != null ) {
				_Settings["onComplete"]();
			}
		}

	}

}